<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/jquery-3-2-1.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    String hb_name = request.getParameter("hbname");
    FindAPrint func =new FindAPrint();
    if (action.equals("del")){//delete hb
        String hb_id = request.getParameter("hbid");
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement = (Statement) connection.createStatement();  //连接实例
        String checksql = "select count(*) as total from hd where HB_HBH='" + hb_id + "';";
        ResultSet rst1 = statement.executeQuery(checksql);
        if (rst1.next()){
            int hdtotal = rst1.getInt("total");
            if (hdtotal > 0) {
%>
<blockquote class="layui-elem-quote layui-quote-nm">无法删除，尚有航段未清除</blockquote>
<%
}
        else {
            String delsql = "delete from hb where HB_HBH='" + hb_id + "';";
            statement.executeUpdate(delsql);
        %>
        <blockquote class="layui-elem-quote layui-quote-nm">删除成功</blockquote>
        <%
                }
            }
    }
    else if (hb_name == null){//未传入航班则输出添加表单
%>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加航班</legend>
</fieldset>
<form class="layui-form" method="post" style="margin: 20px" action="changehb.jsp?action=add">
    <div class="layui-form-item">
        <label class="layui-form-label">航班名称</label>
        <div class="layui-input-block">
            <input type="text" name="hbname" required="required" lay-verify="name" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">飞机机型</label>
        <div class="layui-input-block">
            <input type="text" name="planename" required="required" lay-verify="name" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">航程安排</label>
        <div class="layui-input-block">
            <input type="checkbox" name="airplan" value="0" title="周日">
            <input type="checkbox" name="airplan" value="1" title="周一">
            <input type="checkbox" name="airplan" value="2" title="周二">
            <input type="checkbox" name="airplan" value="3" title="周三">
            <input type="checkbox" name="airplan" value="4" title="周四">
            <input type="checkbox" name="airplan" value="5" title="周五">
            <input type="checkbox" name="airplan" value="6" title="周六">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头等舱数</label>
        <div class="layui-input-block">
            <input type="number" min="1" required="required" name="topnumber" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经济舱数</label>
        <div class="layui-input-block">
            <input type="number" min="1" required="required" name="econumber" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit="" id="submit">确认提交</button>
    </div>
</form>
<script type="text/javascript">
    $(function(){
        $('#submit').click(function(){
            if($("input[name='airplan']:checked").length == 0){
                alert("最少选择一个时间!");
                return false;
            }
            else $('form').submit();
        });
    })
</script>
<%
}
else{
    String driver="com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
    String user="hbpwuser";
    String pass="LFFLXY";
    Class.forName(driver);                   //加载jdbc驱动
    Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

    Statement statement2 = (Statement) connection.createStatement();  //连接实例
    String selectsql2 = "select count(*) as checkname from hb where HB_HBH = '" + hb_name + "';";
    ResultSet rst2 = statement2.executeQuery(selectsql2);
    if (rst2.next()){
        int checkcity = rst2.getInt("checkname");
        if (checkcity > 0){
%>
<blockquote class="layui-elem-quote layui-quote-nm">已有重名航班！</blockquote>
<%
        }
        else {
            String[] checkplan = request.getParameterValues("airplan");
            StringBuilder temp = new StringBuilder("0000000");
            for (int i = 0; i < checkplan.length; i ++){
                int checktemp = Integer.parseInt(checkplan[i]);
                temp.replace(checktemp,checktemp+1,"1");
            }
            String airplan = temp.toString();
            String plane_name = request.getParameter("planename");
            String top_number = request.getParameter("topnumber");
            String eco_number = request.getParameter("econumber");
            Statement state = (Statement) connection.createStatement();  //连接实例
            String insertsql = "insert into hb values('" + hb_name + "','"+ plane_name + "','" + airplan
                    + "'," + top_number + "," + eco_number + ");";
            try {
                state.executeUpdate(insertsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">添加成功</blockquote>
<%
            }catch (SQLException e){
                        out.println(insertsql);
                        out.println(e);
            }
        }
    }
}
    %>
</body>
</html>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.verify({
            name: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9-\\s·]+$").test(value)){
                    return '请输个正常的名称';
                }
            },
            number: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[0-9\\s·]+$").test(value)){
                    return '请输个正常的数字';
                }
            }
        });
        form.render();
    })
</script>

